// Written by Steve Andersen, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
trigger ONEN_ContactChangeInHouseholding on Contact (after delete, after update) {
	//system.debug('ONEN_ContactChangeInHouseholding FIRED');
	List<Id> HHChanging = new List<Id>();
	Map<Id,Contact> HHChangingAddress = new Map<Id,Contact>();
	//list to hold Contacts that need to be updated with new Household Name and Greeting
	List<Id> ContactsWithoutHousehold = new List<Id>();
	//a list of Contacts that have Households and have a valid address
    List<Contact> ContactsWithHouseholdsWithGoodAddress = new List<Contact>();
    
	Contact ContactBeforeChange; 
	
	for (Contact ContactAfterChange : Trigger.isDelete ? Trigger.old : Trigger.new) {
		if (Trigger.IsDelete) {
			HHChanging.add(ContactAfterChange.ONEN_Household__c);
		} else {
			ContactBeforeChange = trigger.oldmap.get(ContactAfterChange.id);
			//if member count changes
			boolean householdChange = ContactBeforeChange.ONEN_Household__c!=ContactAfterChange.ONEN_Household__c;
			//system.debug('householdChange: ' + householdChange);
			boolean nameChange = ContactBeforeChange.FirstName!=ContactAfterChange.FirstName||ContactBeforeChange.LastName!=ContactAfterChange.LastName;
			boolean addressChange = ContactBeforeChange.OtherStreet!=ContactAfterChange.OtherStreet||ContactBeforeChange.OtherCity!=ContactAfterChange.OtherCity||ContactBeforeChange.OtherState!=ContactAfterChange.OtherState||ContactBeforeChange.OtherPostalCode!=ContactAfterChange.OtherPostalCode||ContactBeforeChange.OtherCountry!=ContactAfterChange.OtherCountry||ContactBeforeChange.Home_County__c!=ContactAfterChange.Home_County__c;
			
			boolean householdAdded = false;
			//if the household has changed
			if (householdChange) {
				//if the household started out blank, that means we're coming from an insert
				if (ContactBeforeChange.ONEN_Household__c!=null) {
					
					if (ContactAfterChange.ONEN_Household__c!=null) {
						//if the user didn't null out the household, add the new one to the list
						HHChanging.add(ContactAfterChange.ONEN_Household__c);
						householdAdded = true;	
					} else {
						//if the user did null out the HH, this contact needs a new one
						ContactsWithoutHousehold.add(ContactAfterChange.Id);
					}			
					//add the HH that used to be on this contact to the list of ones that need recalculating
					HHChanging.add(ContactBeforeChange.ONEN_Household__c);
				}
			}
			//if name changed and we didn't already add this household, add it
			if (nameChange&&!householdAdded) {
				HHChanging.add(ContactAfterChange.ONEN_Household__c);
				householdAdded = true;
			}
			if (addressChange) {
				if (!householdAdded) {
					HHChanging.add(ContactAfterChange.ONEN_Household__c);
				}
				HHChangingAddress.put(ContactAfterChange.ONEN_Household__c,ContactAfterChange);			
			}
		}
	} 

	//if we have households that need recalculation
	if(HHChanging.size()>0||ContactsWithoutHousehold.size()>0) {
		//system.debug('ONEN_ContactChangeInHouseholding EXECUTED');
		Map<id,ONEN_Household__c> HHinTrigger = new Map<id,ONEN_Household__c>([Select Id,Name, Greeting__c, Override_Household_Name__c, Override_Greeting__c,MailingStreet__c,MailingCity__c,MailingState__c,MailingPostalCode__c,MailingCountry__c,MailingCounty__c, Member_Count__c from ONEN_Household__c where id IN :HHChanging]);
		ONEN_HouseholdMaintenance ThisHouseholdSet = new ONEN_HouseholdMaintenance(HHinTrigger);
		
		for(ONEN_Household__c hh : HHinTrigger.values()) {
			if (HHChangingAddress.get(hh.id)!=null) {			
				hh.MailingStreet__c = HHChangingAddress.get(hh.id).OtherStreet;
				hh.MailingCity__c = HHChangingAddress.get(hh.id).OtherCity;
				hh.MailingState__c = HHChangingAddress.get(hh.id).OtherState;
				hh.MailingPostalCode__c = HHChangingAddress.get(hh.id).OtherPostalCode;
				hh.MailingCountry__c = HHChangingAddress.get(hh.id).OtherCountry;
				hh.MailingCounty__c = HHChangingAddress.get(hh.id).Home_County__c;
			}
		}
		if(ContactsWithoutHousehold.size()>0) {
			Contact[] ContactsNeedingHousehold = [Select Id, ONEN_Household__c, FirstName, LastName,OtherStreet,OtherCity,OtherState,OtherPostalCode,OtherCountry,Home_County__c,ONEN_Household__r.MailingStreet__c,ONEN_Household__r.MailingCity__c,ONEN_Household__r.MailingState__c,ONEN_Household__r.MailingPostalCode__c,ONEN_Household__r.MailingCountry__c,ONEN_Household__r.MailingCounty__c from Contact where Id IN :ContactsWithoutHousehold];
			//create new HH's
			ThisHouseholdSet.createNewHouseholds(ContactsNeedingHousehold);
			update ContactsNeedingHousehold;
		
		}
		if(HHChanging.size()>0) {		
			ONEN_HouseholdMaintenance ThisHouseholdSet2 = new ONEN_HouseholdMaintenance(HHinTrigger);
			ThisHouseholdSet2.Rename();
		}
		
	} else {
		//system.debug('ONEN_ContactChangeInHouseholding DID NOT EXECUTE');
	}
}